package com.cn.yugan.Activity;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import com.cn.yugan.Engine.Adapter.CollectionListViewAdpater;
import com.cn.yugan.Engine.Http.GetList;
import com.cn.yugan.Engine.Json.ArticleListJson;
import com.cn.yugan.Engine.Beans.ArticleListInfo;
import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.Utils.ToastUtils;
import com.cn.yugan.mobile.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class CollectionActivity extends AppCompatActivity {

    private PullToRefreshListView mPullRefreshListView;
    private CollectionListViewAdpater madapter;
    private ArrayList<ArticleListInfo> mlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);

        initActionbar();
        init();
        loadData();
    }

    private void initActionbar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("我的收藏");
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    private void init() {
        mlist = new ArrayList<>();
        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.collection_lv);
        madapter = new CollectionListViewAdpater(this, mlist);
        mPullRefreshListView.setAdapter(madapter);
        mPullRefreshListView.setMode(PullToRefreshBase.Mode.DISABLED);
//       
//        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener());
//        ILoadingLayout endLabels = mPullRefreshListView.getLoadingLayoutProxy(
//                false, true);
//        endLabels.setPullLabel("上拉我刷新(*^__^*) ");// 刚下拉时，显示的提示    
//        endLabels.setRefreshingLabel("鱼干娘寻找中...");// 刷新时    
//        endLabels.setReleaseLabel("放开我刷新((‵□′)) ");// 下来达到一定距离时，显示的提示
        mPullRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    ArticleListInfo articleListInfo = (ArticleListInfo) madapter.getItem(position-1);
                    Intent intent = new Intent(CollectionActivity.this, ArticleInfoActivity.class);
                    intent.putExtra("aid", articleListInfo.getAid());
                    startActivity(intent);
                
            }
        });
    }
    private void loadData(){
        GetList.FavoriteListAsyncHttpClient(this, getString(R.string.serverurl) + getString(R.string.like), new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int i, Header[] headers, byte[] bytes) {
                        try {
                            LogUtils.d(new String(bytes, "UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        mlist = ArticleListJson.json2list(bytes);
                        madapter.refreshList(mlist);
                        madapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                        ToastUtils.show(CollectionActivity.this, "服务器未响应");
                    }
                }

        );
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_nomal_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return true;
    }
    //刷新逻辑 暂时无用
//    private class OnRefreshListener implements PullToRefreshBase.OnRefreshListener<ListView> {
//        @Override
//        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
//            new GetDataTask().execute();
//        }
//
//    }
//
//    private class GetDataTask extends AsyncTask<Void, Void, String[]> {
//
//        @Override
//        protected String[] doInBackground(Void... params) {
//            // Simulates a background job.
//            try {
//                Thread.sleep(4000);
//            } catch (InterruptedException e) {
//            }
//            return null;
//        }
//
//        @Override
//        protected void onPostExecute(String[] result) {
//            // mAdapter.notifyDataSetChanged();
//
//            // Call onRefreshComplete when the list has been refreshed.
//            mPullRefreshListView.onRefreshComplete();
//            super.onPostExecute(result);
//        }
//
//    }
}
